package com.czk.utlls;

import com.alibaba.fastjson.JSONObject;
import com.czk.pojo.card.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/25 10:07
 */
@Component
public class CardUtil {
    @Resource
    private TokenUtil tokenUtil;
    // 上传卡券图片接口
    private String uploadCardPicUrl = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN";

    /**
     * 上传卡券logo
     */
    public void uploadCardPic() {
        uploadCardPicUrl = uploadCardPicUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        String result = HttpUtil.uploadPic(uploadCardPicUrl, "E:\\pic\\alibaba.jpg");
        // {"url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/YATTsEfwU9gia7GK6cAA9cxPztvPGCdcBNx45tzA8pn18ISUCP9SySUesWLoejJib9Vgdib2cW7Ba3HkrfO1sziarQ\/0"}
        System.out.println(result);
    }

    private String createCardUrl = "https://api.weixin.qq.com/card/create?access_token=ACCESS_TOKEN";

    public void createCard() {
        Card card = new Card();
        card.setCard_type("GROUPON");
        Groupon groupon = new Groupon();
        BaseInfo baseInfo = new BaseInfo();
        baseInfo.setLogo_url("http://mmbiz.qpic.cn/YATTsEfwU9gia7GK6cAA9cxPztvPGCdcBNx45tzA8pn18ISUCP9SySUesWLoejJib9Vgdib2cW7Ba3HkrfO1sziarQ/0");
        baseInfo.setBrand_name("微信餐厅");
        /**
         * code_type": "CODE_TYPE_TEXT",
         *               "title": "132元双人火锅套餐",
         *               "color": "Color010",
         *               "notice": "使用时向服务员出示此券",
         *               "service_phone": "020-88888888",
         *               "description": "不可与其他优惠同享\n如需团购券发票，请在消费时向商户提出\n店内均可使用，仅限堂食",
         */
        baseInfo.setCode_type("CODE_TYPE_TEXT");
        baseInfo.setTitle("132元双人火锅套餐");
        baseInfo.setColor("Color010");
        baseInfo.setNotice("使用时向服务员出示此券");
        baseInfo.setService_phone("15723315609");
        baseInfo.setDescription("不可与其他优惠同享如需团购券发票，请在消费时向商户提出店内均可使用，仅限堂食");
        /**
         * date_info": {
         *                   "type": "DATE_TYPE_FIX_TIME_RANGE",
         *                   "begin_timestamp": 1397577600,
         *                   "end_timestamp": 1472724261
         *               },
         */
        DataInfo dataInfo = new DataInfo();
        dataInfo.setType("DATE_TYPE_FIX_TIME_RANGE");
        dataInfo.setBegin_timestamp(1397577600L);
        dataInfo.setEnd_timestamp(1472724261L);
        baseInfo.setData_info(dataInfo);
        Sku sku = new Sku();
        sku.setQuantity(500000L);
        baseInfo.setSku(sku);
        groupon.setBase_info(baseInfo);
        card.setGroupon(groupon);
        String jsonString = JSONObject.toJSONString(card);
        System.out.println(jsonString);
        createCardUrl = createCardUrl.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        // 发送请求
        System.out.println(WeChatUtil.post(createCardUrl, jsonString));
    }
}
